package com.bishe.demo.service;

import com.bishe.demo.mapper.VipMapper;
import com.bishe.demo.pojo.Vip;
import com.bishe.demo.pojo.vo.Status;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class VipService {

    @Autowired
    VipMapper vipMapper;
    //把vip信息返回给页面
    public List<Vip> selectVip(){

        List<Vip> list = vipMapper.selectList(null);
        return list;
    }
    //根据vipId删除Vip
    public Status deleteVipById(Integer vipId){
        Status status = new Status();
        if (vipId==null){
            status.setMsg("没有可删除的VIP！");
            status.setCode(false);
            return status;
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("vip_id",vipId);
        vipMapper.deleteByMap(map);
        status.setCode(true);
        status.setMsg("删除成功！");
        return status;

    }

    //更新Vip消息
    public Status updateVip(Integer vipId,String vipName){
        Status status = new Status();
        if (vipId==null){
          status.setMsg("没有可修改的vip！");
          status.setCode(true);
          return status;
        }
        if (StringUtils.isBlank(vipName)){
            status.setMsg("用户名不能为空！");
            status.setCode(false);
            return status;
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("vip_name",vipName);
       List<Vip> vipList = vipMapper.selectByMap(map);
       //如果数据中没有该没有字直接插入
       if (vipList.isEmpty()){
           Vip vip = new Vip();
           vip.setVipId(vipId);
           vip.setVipName(vipName);
           vipMapper.updateById(vip);
       }else {
           status.setMsg("该名字已经存在！");
           status.setCode(false);
           return status;
       }

       status.setMsg("修改成功！");
       status.setCode(true);
       return status;
    }
    //添加vip信息
    public Status addVip(String vipName){
        Status status = new Status();
        if (StringUtils.isBlank(vipName)){
            status.setMsg("vip名不能为空！");
            status.setCode(false);
            return status;
        }

        HashMap<String,Object> map = new HashMap<>();
        map.put("vip_name",vipName);
        List<Vip> vipList = vipMapper.selectByMap(map);
        //如果数据中没有该没有字直接插入
        if (vipList.isEmpty()){
            Vip vip = new Vip();
            vip.setVipName(vipName);
            vipMapper.insert(vip);
        }else {
            status.setMsg("该名字已经存在！");
            status.setCode(false);
            return status;
        }

        status.setMsg("添加成功！");
        status.setCode(true);
        return status;
    }
}
